package de.dailab.graphworld.bean.memory;

import de.dailab.graphworld.bean.Notifiable;
import de.dailab.jiactng.agentcore.comm.message.JiacMessage;
import de.dailab.jiactng.agentcore.knowledge.IFact;
import org.sercho.masp.space.event.SpaceEvent;
import org.sercho.masp.space.event.SpaceObserver;
import org.sercho.masp.space.event.WriteCallEvent;

/**
 * A message observer implemented according to the simple visitor pattern.
 * Handles the casts and instanceof-checks due to non-conform generic
 * implementation of space events.
 * 
 * @see {@link Notifiable}
 *
 * @author Kseniya
 */
public class Notifier implements SpaceObserver<IFact> {

    private final Notifiable owner;

    public Notifier(Notifiable owner) {
        this.owner = owner;
    }

    @Override
    public void notify(SpaceEvent<? extends IFact> event) {

        if (event instanceof WriteCallEvent) {
            Object object = ((WriteCallEvent) event).getObject();
            if (object instanceof JiacMessage) {
                JiacMessage transport = (JiacMessage) object;
                owner.consume(transport);
            }
        }
    }
}
